package com.example.book_subscribe.bookList.controlle;

import com.example.book_subscribe.bookList.dto.*;
import com.example.book_subscribe.bookList.mapper.BookListMapper;
import com.example.book_subscribe.bookList.result.*;
import com.example.book_subscribe.common.utils.ResponseResult;
import com.example.book_subscribe.login.dto.UserDto;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@RestController
public class BookListControlle {
    @Resource
    private BookListMapper BookListMapper;
    @PostMapping("/BookList")
    public ResponseResult<?> getBookList(@RequestBody BookListDto inputAll) {
        long offset=(inputAll.getPage()-1)*inputAll.getPageSize();
        ArrayList<BookResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.searchBookList(inputAll, offset);
        return ResponseResult.okResult(bookResults);
    }
    @GetMapping("/top-books-by-category")
    public ResponseResult<?> getTopBooksByCategory() {
        ArrayList<CategoryTopBooksResult> results = BookListMapper.selectTopBooksByCategory();
        return ResponseResult.okResult(results);
    }
    @PostMapping("/BookDetail")
    public ResponseResult<?> getBookDetail(@RequestParam String bookId) {
        ArrayList<BookResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.searchBookDetail(bookId);
        return ResponseResult.okResult(bookResults);
    }
    @PostMapping("/RandomBook")
    public ResponseResult<?> getRandomBook(@RequestParam String bookId) {
        ArrayList<BookResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.getRandomBook(bookId);
        return ResponseResult.okResult(bookResults);
    }
    @PostMapping("/SaveComment")
    public ResponseResult<?> getBookComment(@RequestBody BookCommentDto bookComment) {

        boolean bookResults=BookListMapper.searchOpenid(bookComment.getOpenId(),bookComment.getBookId());
        if(!bookResults){
            BookListMapper.insertComment(bookComment);
            return ResponseResult.okResult(200,"请求成功");
        }else {
            BookListMapper.updateComment(bookComment);
            return ResponseResult.errorResult(404,"请求失败");
        }
    }
    @PostMapping("/UserComments")
    public String getUserComments(@RequestBody BookCommentDto bookComment) {
        String bookResults=BookListMapper.searchUserComments(bookComment.getOpenId(),bookComment.getBookId());
        return bookResults;
    }
    @PostMapping("/UserLastComments")
    public String getLastComment(@RequestBody BookCommentDto bookComment) {
        String bookResults=BookListMapper.searchUserLastComments(bookComment.getOpenId(),bookComment.getBookId());
        return bookResults;
    }
    @GetMapping("/CommentsProgress")
    public ResponseResult<?> getCommentsProgress(@RequestParam String bookId) {
        long allNumber=BookListMapper.searchAllComments(bookId);
        long goodNumber=BookListMapper.searchGoodComments(bookId);
        long ordinaryNumber=BookListMapper.searchOrdinaryComments(bookId);
        long badNumber=BookListMapper.searchBadComments(bookId);
        // 防止除以零的情况，若总评论数为零，则好评率、一般评价率和差评率都设为零
        double goodRate = allNumber > 0 ? ((double) goodNumber / allNumber) * 100 : 0;
        double ordinaryRate = allNumber > 0 ? ((double) ordinaryNumber / allNumber) * 100 : 0;
        double badRate = allNumber > 0 ? ((double) badNumber / allNumber) * 100 : 0;

        // 保留两位小数
        goodRate = Math.round(goodRate * 100.0) / 100.0;
        ordinaryRate = Math.round(ordinaryRate * 100.0) / 100.0;
        badRate = Math.round(badRate * 100.0) / 100.0;

        // 将计算结果返回给前端
        Map<String, Object> result = new HashMap<>();
        result.put("totalComments", allNumber);
        result.put("goodRate", goodRate);
        result.put("ordinaryRate", ordinaryRate);
        result.put("badRate", badRate);
        return ResponseResult.okResult(result);
    }
    @PostMapping("/AllComments")
    public ResponseResult<?> getAllComments(@RequestBody BookCommentLikeDto bookCommentLike) {
        ArrayList<BookCommentsResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.getAllComments(bookCommentLike.getBookId(),bookCommentLike.getOpenidUser());
        return ResponseResult.okResult(bookResults);
    }
    @PostMapping("/UpdateCommentList")
    public String updateCommentList(@RequestBody BookCommentLikeDto bookCommentLike) {
        if(bookCommentLike.getState()){
            BookListMapper.addCommentList(bookCommentLike.getBookId(),bookCommentLike.getOpenidUser());
        }else{
            BookListMapper.subtractCommentList(bookCommentLike.getBookId(),bookCommentLike.getOpenidUser());
        }
        return "true";
    }
    @PostMapping("/UpdateCommentLike")
    public String updateCommentLike(@RequestBody BookCommentStateDto bookCommentState) {
        boolean bookResults=BookListMapper.searchCommentLike(bookCommentState);
        if(!bookResults){
            BookListMapper.insertCommentLike(bookCommentState);
        }else {
            BookListMapper.updateCommentLike(bookCommentState);
        }
        return "true";
    }

    @GetMapping("/BookFileUrl")
    public ResponseResult<?> getBookFileUrl(@RequestParam String bookId) {
        String bookResults=BookListMapper.searchBookFileUrl(bookId);
        return ResponseResult.okResult(bookResults);
    }
    @PostMapping("/saveProgress")
    public String saveProgress(@RequestBody BookProgressDto bookProgress) {
        boolean bookResults=BookListMapper.searchProgress(bookProgress);
        if(!bookResults){
            BookListMapper.insertProgress(bookProgress);
        }else {
            BookListMapper.updateProgress(bookProgress);
        }
        return "true";
    }
    @PostMapping("/getProgress")
    public BookProgressResult getProgress(@RequestBody BookProgressDto bookProgress) {
        BookProgressResult bookResults=BookListMapper.getProgress(bookProgress);
        return bookResults;
    }
    @PostMapping("/saveNote")
    public String saveNote(@RequestBody BookNoteDto bookNote) {
        boolean bookResults=BookListMapper.searchNote(bookNote);
        if(!bookResults){
            BookListMapper.insertNote(bookNote);
        }else {
            BookListMapper.updateNote(bookNote);
        }
        return "true";
    }
    @PostMapping("/getNote")
    public String getNote(@RequestBody BookNoteDto bookNote) {
        String bookResults=BookListMapper.getNote(bookNote);
        return bookResults;
    }
    @PostMapping("/saveSubscribe")
    public String saveSubscribe(@RequestBody BookSubscribeDto bookSubscribe) {
        boolean bookResults=BookListMapper.searchSubscribe(bookSubscribe);
        if(!bookResults){
            BookListMapper.insertSubscribe(bookSubscribe);
        }else {
            BookListMapper.updateSubscribe(bookSubscribe);
        }
        return "true";
    }
    @PostMapping("/getSubscribe")
    public Boolean getSubscribe(@RequestBody BookSubscribeDto bookSubscribe) {
        Boolean bookResults=BookListMapper.getSubscribe(bookSubscribe);
        return bookResults;
    }
    @GetMapping("/getAllClassify")
    public ArrayList<BookClassifyResult> getAllClassify(@RequestParam String classify) {
        ArrayList<BookClassifyResult> results=BookListMapper.getAllClassify(classify);
        return results;
    }
    @PostMapping("/getClassifyBooks")
    public ArrayList<BookResult> getClassifyBooks(@RequestBody BookClassifyDto bookClassify) {
        if(bookClassify.getCategory_type()!=0){
        ArrayList<BookResult> results=BookListMapper.getClassifyBooks(bookClassify.getCategory_type());
        return results;
        }else {
            ArrayList<BookResult> results=BookListMapper.getClassifyAllBooks(bookClassify.getClassify());
            return results;
        }
    }
    @PostMapping("/saveSearchHistory")
    public String saveSearchHistory(@RequestBody BookSubscribeDto bookSubscribe) {
        boolean bookResults=BookListMapper.searchHistory(bookSubscribe);
        if(!bookResults && bookSubscribe.getBookName() != null && !bookSubscribe.getBookName().isEmpty()){
            BookListMapper.insertHistory(bookSubscribe);
        }else if (bookSubscribe.getBookName() != null && !bookSubscribe.getBookName().isEmpty()){
            BookListMapper.updateHistory(bookSubscribe);
        }
        return "true";
    }

    @PostMapping("/getRecommendBooks")
    public ArrayList<BookResult> getRecommendBooks(@RequestBody BookListDto inputAll) {
        long offset=(inputAll.getPage()-1)*inputAll.getPageSize();
        ArrayList<BookResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.recommendBooks(inputAll.getInputText(),inputAll, offset);
        return bookResults;
    }
    @PostMapping("/getRankBooks")
    public ArrayList<BookResult> getRankBooks(@RequestBody BookListDto inputAll) {
        long offset=(inputAll.getPage()-1)*inputAll.getPageSize();
        ArrayList<BookResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.getRankBooks(inputAll, offset);
        return bookResults;
    }
    @PostMapping("/getReadHistory")
    public ArrayList<BookResult> getReadHistory(@RequestParam String openid) {
        ArrayList<BookResult> bookResults = new ArrayList<>();
        bookResults=BookListMapper.searchReadHistory(openid);
        return bookResults;
    }
}
